//var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');

Ext.define('AM.view.Crud', {
    extend:'Ext.grid.Panel',
    alias:'widget.usercrud',
    id:'usercrud',
    selType:'rowmodel',
    rowEditor:Ext.create('Ext.grid.plugin.RowEditing', {
        clicksToEdit:2,
        listeners:{
            canceledit:function (editor, event, eOpts) {
                interanlId = parseInt(event.record.internalId);
                if (!(interanlId > 0)) {
                    event.grid.store.remove(event.record);
                }
            },
            edit:function (editor, e) {
                var grid = e.grid;
                var record = e.record;
//                e.grid.store.load();

//                console.log(record.data)
// ;
            }
        }
    }),
    store:'UsersStore',
    frame:true,
    title:'All users',
    initComponent:function () {
        var me = this;

        Ext.applyIf(me, {
            columns:[
                {
                    dataIndex:'name',
                    flex:1,
                    header:'Name',
                    editor:{
                        xtype:'textfield',
                        allowBlank:true
                    }
                },
                {
                    dataIndex:'surname',
                    flex:1,
                    header:'Apellido',
                    editor:{
                        xtype:'textfield',
                        allowBlank:true
                    }
                },
                {
                    dataIndex:'birthday',
                    flex:1,
                    header:'Cumple',
                    editor:{
                        xtype:'datefield',
                        allowBlank:true
                    }
                },
                {
                    dataIndex:'email',
                    flex:1,
                    header:'Email',
                    editor:{
                        xtype:'textfield',
                        allowBlank:true
                    }
                }
            ],
            dockedItems:[
                {
                    xtype:'toolbar',
                    items:[
                        {
                            text:'Add',
                            iconCls:'icon-add',
                            handler:function () {
                                me.store.insert(0, Ext.create('AM.model.User'));
                                me.rowEditor.startEdit(0, 0);
                            }
                        },
                        '-',
                        {
                            itemId:'delete',
                            text:'Delete',
                            iconCls:'icon-delete',
                            disabled:true,
                            handler:function () {
                                var selection = me.getSelectionModel().getSelection()[0];
                                if (selection) {
                                    me.store.remove(selection);
                                }
                            }
                        }
                    ]
                }
            ],
            viewConfig:{

            }
        });
        me.plugins = [me.rowEditor];
        me.callParent(arguments);
    },
    listeners:{
        selectionchange:function (selModel, selections) {
            this.down('#delete').setDisabled(selections.length === 0);
        }
    }
})
;